
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.LinkedList;
import logic.Utils;
import model.Documento;
import org.icepdf.core.pobjects.Document;
import persistence.SqlConnector;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author 
 */
public class Main2 {

    public static void main(String[] args) throws Exception {
        SqlConnector connector = new SqlConnector();
        connector.connect();
        Documento doc = null;
        for(int i=19; i<6099; i++){
            ResultSet resultDoc = connector.executeStatementQuery("SELECT * FROM Documento WHERE id = "+i);
            if(resultDoc.next()){
                doc = new Documento(resultDoc.getInt("id"), resultDoc.getString("nombre"), resultDoc.getString("path"));
                File docFile = new File(doc.getPath());
                LinkedList<String> listaPalabras = new LinkedList<String>();
                if(obtenerGraficoExtension(docFile.getName()) ==1){
                    Document docPDF = new Document();
                    docPDF.setFile(doc.getPath());
                    for (int j = 0; j < docPDF.getNumberOfPages(); j++) {
                       String caracteresPagina = Utils.limpiarAcentos(docPDF.getPageText(j).toString());
                       listaPalabras.addAll(Utils.separarPalabras(caracteresPagina));
                    }
                    System.out.println("El doc: "+docFile.getName() + " tiene " + listaPalabras.size() + " palabras");
                }
                else{
                    FileReader docTxt;
                    try {
                        docTxt = new FileReader(docFile);
                        BufferedReader br = new BufferedReader(docTxt);
                        while (br.ready()) {
                            String caracteresPagina = Utils.limpiarAcentos(br.readLine());
                            listaPalabras.addAll(Utils.separarPalabras(caracteresPagina));
                        }
                    }
                    catch (IOException ex) {
                    }
                    System.out.println("El doc: "+docFile.getName() + " tiene " + listaPalabras.size() + " palabras");
                }
                listaPalabras = new LinkedList<String>();
                //connector.executeStatement("UPDATE Documento SET cantPalabras = "+docFile.length()+" WHERE id = "+doc.getId());
            }
        }
    }

    private static int obtenerGraficoExtension (String nombreArchivo){
        String ext = nombreArchivo.substring(nombreArchivo.lastIndexOf(".") + 1).toLowerCase();
        System.out.println(ext);
        if(ext.compareTo("pdf") == 0){
            return 1;
        } else {
            return 2;
        }
    }
}
